package org.ariadne_eu.hcifetcher.mumeUtils;

import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Vector;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.ariadne_eu.hcifetcher.rest.RestUtils;
import org.ariadne_eu.hcifetcher.util.DateManager;
import org.joda.time.format.DateTimeFormatter;
import org.json.JSONException;
import org.json.JSONObject;

@Path("/entries/stream")
public class Stream {
	
	@Path("/")
	@GET
	@Produces("application/json")
	public String getStreamOfStudent(@QueryParam("studentid") String studentid,
									 @QueryParam("courseid") String courseid,
									 @QueryParam("offset") String offset,
									 @QueryParam("size") String size){
		int intoffset = 0;
		int intsize = 20;
		
		try{
			intsize = Integer.parseInt(size);
		}
		catch(NumberFormatException e){	}
		try{
			intoffset = Integer.parseInt(offset);
		}
		catch(NumberFormatException e){	}
		
		List<JSONObject> entities = new Vector<JSONObject>();
		Vector<JSONObject> courses;
		if(courseid == null)
			courses = getCoursesOfStudent(studentid, 0, 1000);
		else
			courses = getOneCourseOfStudent(studentid, courseid, 0, 1000);
		for(JSONObject course : courses){
			Map<String, Object> filter = new HashMap<String, Object>();
			try {
				filter.put("set", course.getString("hashtag"));
				entities.addAll(RestUtils.getInstance().getEntriesJSON("Tweet", filter, 0, 20, "datestamp", false));
				entities.addAll(RestUtils.getInstance().getEntriesJSON("Blog", filter, 0, 20, "datestamp", false));
				//entities.addAll(RestUtils.getInstance().getEntriesJSON("Comment", filter, 0, 1000, "datestamp", false));
				filter.clear();
				filter.put("courseid", course.getString("id"));
				Vector<JSONObject> questions = RestUtils.getInstance().getEntriesJSON("Question", filter, 0, 20, "datestamp", false);
				for(JSONObject question : questions){
					try {
						question.append("student", RestUtils.getInstance().getEntityById(question.getString("studentid")));
						question.append("course", RestUtils.getInstance().getEntityById(question.getString("courseid")));
					} catch (JSONException e) {
						e.printStackTrace();
					}
				}
				entities.addAll(questions);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		sortJSONByDateStamp(entities);
		if(intoffset!=0){
			intsize = Math.min((entities.size() - intoffset -1), intsize);
			if(intsize<=0)
				intsize=1;
			entities = entities.subList(intoffset, intoffset+intsize);
		}
		else if(intsize != 0){
			intsize = Math.min(entities.size(), intsize);
			entities = entities.subList(0, intsize);
		}
		return RestUtils.getInstance().JSONToString(new Vector<JSONObject>(entities));
	}
	
	public void sortJSONByDateStamp(List<JSONObject> entities){
		Comparator<JSONObject> datestampComparator = new Comparator<JSONObject>(){
			DateTimeFormatter dtf = DateManager.fmt1;
			@Override
			public int compare(JSONObject o1, JSONObject o2) {
				String s1 = null;
				String s2 = null;
				try {
					s1 = o1.getString("datestamp");
					s2 = o2.getString("datestamp");
				} catch (JSONException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				Date d1 = dtf.parseDateTime(s1).toDate(),
					 d2 = dtf.parseDateTime(s2).toDate();
				return d2.compareTo(d1);
			}
		};
		Collections.sort(entities, datestampComparator);
	}
	
	public Vector<JSONObject> getCoursesOfStudent(String studentid, int intoffset, int intsize) {
		Map<String, Object> filters = new HashMap<String, Object>();
		filters.put("studentid", studentid);
		Vector<JSONObject> enrollments = RestUtils.getInstance().getEntriesJSON("Enrollment", filters,intoffset, intsize, "courseid", false);
		Vector<JSONObject> courses = new Vector<JSONObject>();
		for(JSONObject enrollment : enrollments){
			JSONObject course = null;
			try {
				course = RestUtils.getInstance().getEntityById(enrollment.getString("courseid"));
			} catch (JSONException e) {
			}
			courses.add(course);
		}
		return courses;
	}
	
	public Vector<JSONObject> getOneCourseOfStudent(String studentid, String courseid, int intoffset, int intsize) {
		Map<String, Object> filters = new HashMap<String, Object>();
		filters.put("studentid", studentid);
		filters.put("courseid", courseid);
		Vector<JSONObject> enrollments = RestUtils.getInstance().getEntriesJSON("Enrollment", filters,intoffset, intsize, "courseid", false);
		Vector<JSONObject> courses = new Vector<JSONObject>();
		for(JSONObject enrollment : enrollments){
			JSONObject course = null;
			try {
				course = RestUtils.getInstance().getEntityById(enrollment.getString("courseid"));
			} catch (JSONException e) {
			}
			courses.add(course);
		}
		return courses;
	}
}
